package com.wd.live.platform;

import java.util.LinkedList;
import java.util.List;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectors;
import us.codecraft.webmagic.selector.XpathSelector;

public class HuyaRepoPageProcessor  extends AbstractProcessor {
	private Pipeline pipeline;
	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);
	
	 public HuyaRepoPageProcessor(Pipeline pipeline){
		  this.pipeline = pipeline;
	  }
	 private HuyaRepoPageProcessor(){
	  }

	public void process(Page page) {
		List<String> list = page.getHtml().xpath("//div[@id='js-video-all']/ul/li/span[@class='txt']/html()").all();
		List<String[]> result = new LinkedList<String[]>();
		for (String str : list) {
			String[] array = new String[4];
			XpathSelector xpathSelector = Selectors.xpath("//span[@class='game-type fr']/a/html()");
			String gameName = new Html(xpathSelector.selectList(str), false).toString();
			array[0] = gameName;

			xpathSelector = Selectors.xpath("//span[@class='avatar fl']/i/html()");
			String userName = new Html(xpathSelector.selectList(str), false).toString();
			array[1] = userName;

			xpathSelector = Selectors.xpath("//span[@class='num fr']/i[@class='js-num']/html()");
			String total = new Html(xpathSelector.selectList(str), false).toString();
			array[2] = total;
			
			array[3] = "虎牙";

			result.add(array);
		}

		page.putField("listDate", result);
	}

	public Site getSite() {
		return site;
	}

	public void execute() {
		Spider.create(new HuyaRepoPageProcessor())
		.addUrl("http://www.huya.com/l")
		.addPipeline(pipeline)
				// 开启5个线程抓取
				.thread(5)
				// 启动爬虫
				.run();
	}
}